package com.javaweb.admin.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javaweb.admin.entity.KdlGM;
import com.javaweb.admin.entity.KdlSaleOutQc;
import com.javaweb.admin.entity.NcTaskLog;
import com.javaweb.admin.entity.Scheduled;
import com.javaweb.admin.mapper.ScheduledMapper;
import com.javaweb.admin.query.KdlGMQuery;
import com.javaweb.admin.query.KdlSaleOutQcQuery;
import com.javaweb.admin.service.AbstractJob;
import com.javaweb.admin.service.IKdlGMService;
import com.javaweb.admin.service.IKdlSaleOutQcService;
import com.javaweb.admin.service.INcTaskLogService;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.common.utils.StringUtils;
import com.seegw.shaphar.IKDLService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.javaweb.admin.constant.KdlCallConstant.DOCTYPE.DOCTYPE_GM;
import static com.javaweb.admin.constant.KdlCallConstant.DOCTYPE.DOCTYPE_Z04KDL;

@Component
public class SyncKdlGM2NC extends AbstractJob {
    private static final Logger log = LoggerFactory.getLogger(SyncKdlPurchase2NC.class);
    @Autowired
    ScheduledMapper scheduledMapper;

    @Autowired
    IKDLService kdlService;

    @Autowired
    private IKdlGMService kdlGMService;

    @Autowired
    INcTaskLogService iNcTaskLogService;


    @Override
    public String getName() {
        QueryWrapper<Scheduled> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classpath",this.getClass().getName());
        Scheduled entity = scheduledMapper.selectOne(queryWrapper);
        return entity.getName();
    }

    @Override
    public void execute() {
        //获取康德乐采购入库质检回传
        KdlGMQuery entity = new KdlGMQuery();
        entity.setPrincipalid(CommonConfig.kdlCompanyId);
        entity.setDoctype(DOCTYPE_GM.value());
        entity.setStarttime(kdlGMService.GetMaxSyncTime());
        entity.setEndtime(DateUtils.getTime());
        try {
            log.info("开始获取康德乐移库回传....");
            kdlGMService.syncGM(entity);
            log.info("获取康德乐移库回传完成");
        }
        catch(Exception ex){
            log.error(ex.getMessage());
        }
        //获取未完成上传的销售出库质检记录
        List<KdlGM> returnList = kdlGMService.getGMSubmitList();
        NcTaskLog taskLog = new NcTaskLog();
        taskLog.setTaskType("移库回传NC");
        taskLog.setBillCount(returnList.size());
        taskLog.setStartTime(DateUtils.now());
        String billno = "";
        String remark = "";

        log.info(String.format("开始回写NC移库,获取到%d个待回写记录....",returnList.size()));
        JsonResult ret;
        for (KdlGM result: returnList) {
            try {
                if(StringUtils.isEmpty(result.getCompanycode())){
                    //若组织编码为空，需要人工干预补充组织编码
                    continue;
                }
                ret = kdlService.submitWhstransToNC(result.getOrderno());
                if(ret.getCode()==0){
                    remark = "物料凭证号:"+result.getOrderno()+"回写NC成功";
                    log.info(remark);
                }
                else {
                    remark = "物料凭证号:"+result.getOrderno()+"回写NC失败:"+ret.getMsg();
                    log.info(remark);
                }
                if(!billno.contains(result.getOrderno())) {
                    billno += result.getOrderno() + ",";
                }
            }catch(Exception ex)
            {
                log.error(ex.getMessage());
            }
        }
        taskLog.setRemark(remark);
        taskLog.setBillNo(billno);
        taskLog.setEndTime(DateUtils.now());
        iNcTaskLogService.save(taskLog);


    }
}
